﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;

namespace StateMachineXNA
{
    class StateMachine<T> where T : IState
    {
        private Stack<T> states;
        private Game1 game;

        public StateMachine(T start_state, Game1 game)
        {
            this.game = game;
            states = new Stack<T>();
            Push(start_state);
        }

        public void Push(T state)
        {
            if (states.Count > 0)
                states.Peek().Pause();

            states.Push(state);
            state.Init(game);
        }

        public void Pop()
        {
            if (states.Count > 0)
            {
                states.Pop();

                if (states.Count > 0)
                    states.Peek().Resume();
            }
        }

        public void Change(T state)
        {
            if (states.Count > 0)
            {
                states.Pop();
                state.Init(game);
                states.Push(state);
            }
        }

        public T Current
        { get { return states.Peek(); } }

        public int Count
        { get { return states.Count; } }
    }
}
